今天我們來介紹裝飾器的使用方法跟種類:
from odoo import models, fields, api
class Product(models.Model):
_name = 'custom.product'
_description = 'Custom Product Model'
name = fields.Char(string='Product Name', required=True)
stock_quantity = fields.Integer(string='Stock Quantity', default=0)
price = fields.Float(string='Price')
@api.constrains('stock_quantity')
def _check_stock_quantity(self):
for product in self:
if product.stock_quantity < 0:
raise models.ValidationError("Stock quantity cannot be negative!")
@api.depends('stock_quantity')
def _compute_value(self):
for product in self:
product.value = product.stock_quantity * product.price
@api.onchange('price')
def _on_price_change(self):
if self.price < 0:
self.price = 0
@api.model
def create(self, vals):
if 'stock_quantity' in vals and vals['stock_quantity'] < 0:
vals['stock_quantity'] = 0
return super(Product, self).create(vals)
明天我們接著介紹 Environment 的類型